# Support libraries for KiCad

_realname=kicad
pkgbase=mingw-w64-${_realname}-library
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}-footprints"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-symbols"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-templates"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-packages3D")
pkgver=9.0.2
pkgrel=1
pkgdesc="Support libraries for KiCad (mingw-w64)"
arch=('any')
mingw_arch=('ucrt64' 'clang64' 'clangarm64')
url='https://www.kicad.org/'
msys2_repository_url="https://gitlab.com/kicad/libraries"
msys2_references=(
  'archlinux: kicad-library'
)
license=("spdx:GPL-3.0-or-later")
groups=("${MINGW_PACKAGE_PREFIX}-eda")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
options=('!strip')
source=("https://gitlab.com/kicad/libraries/kicad-footprints/-/archive/${pkgver}/kicad-footprints-${pkgver}.tar.bz2"
        "https://gitlab.com/kicad/libraries/kicad-symbols/-/archive/${pkgver}/kicad-symbols-${pkgver}.tar.bz2"
        "https://gitlab.com/kicad/libraries/kicad-templates/-/archive/${pkgver}/kicad-templates-${pkgver}.tar.bz2"
        "https://gitlab.com/kicad/libraries/kicad-packages3D/-/archive/${pkgver}/kicad-packages3D-${pkgver}.tar.bz2")
sha256sums=('e95e015287d68491c17b63f800a3db7639c151d2e8ec432561c8c8b2d1afadd0'
            '3a15138ac603937f8e144b045908823c6ddf0274e71a8c9745a68894b4be594c'
            '0e23c346f5fdfbff873c388eabe2dc8dfef9794fd40b3ba392255f02f8e3ba06'
            '5d969971df4957972fb0aadae5bd03c4d654f6aacf7a95d972f49ac56ea6aee4')

build() {
  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  for _s in "footprints" "symbols" "templates" "packages3D"; do
    msg2 "Build kicad-${_s}"
    mkdir -p "${srcdir}/build-${_s}-${MSYSTEM}" && cd "${srcdir}/build-${_s}-${MSYSTEM}"

    MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
      "${MINGW_PREFIX}"/bin/cmake.exe \
        -GNinja \
        -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
        "${_extra_config[@]}" \
        ../${_realname}-${_s}-${pkgver}

    "${MINGW_PREFIX}"/bin/cmake.exe --build .
  done
}

package_footprints() {
  pkgdesc="KiCad footprints library (mingw-w64)"
  conflicts=(${MINGW_PACKAGE_PREFIX}-${_realname}-footprints-git)
  replaces=(${MINGW_PACKAGE_PREFIX}-${_realname}-footprints-git)
  optdepends=("${MINGW_PACKAGE_PREFIX}-${_realname}")

  cd "${srcdir}/build-footprints-${MSYSTEM}"

  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .
}

package_symbols() {
  pkgdesc="KiCad symbols library (mingw-w64)"
  conflicts=(${MINGW_PACKAGE_PREFIX}-${_realname}-symbols-git)
  replaces=(${MINGW_PACKAGE_PREFIX}-${_realname}-symbols-git)
  optdepends=("${MINGW_PACKAGE_PREFIX}-${_realname}")

  cd "${srcdir}/build-symbols-${MSYSTEM}"

  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .
}

package_templates() {
  pkgdesc="KiCad templates library (mingw-w64)"
  conflicts=(${MINGW_PACKAGE_PREFIX}-${_realname}-templates-git)
  replaces=(${MINGW_PACKAGE_PREFIX}-${_realname}-templates-git)

  cd "${srcdir}/build-templates-${MSYSTEM}"

  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .
}

package_packages3D() {
  pkgdesc="KiCad 3D models library (mingw-w64)"
  conflicts=(${MINGW_PACKAGE_PREFIX}-${_realname}-packages3D-git)
  replaces=(${MINGW_PACKAGE_PREFIX}-${_realname}-packages3D-git)

  cd "${srcdir}/build-packages3D-${MSYSTEM}"

  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-${_realname}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
